<template>
  <div class="yytx-container">
    <!-- 搜索框 -->
    <div class="search-bar" @click="goToYpxx">
      <img class="search-icon" src="/static/UtilsImage/Search.png" />
      <input class="search-input" placeholder="不记得药名？去找药" />
    </div>

    <!-- 我的提醒标题 -->
    <div class="section-title">
      <span class="section-bar"></span>
      <span class="section-title-text">我的提醒</span>
    </div>

    <!-- 提醒卡片列表 -->
    <div v-for="item in reminders" :key="item.rid" class="remind-card">
      <div class="remind-row">
        <span class="remind-bar"></span>
        <span class="remind-title">{{ item.medname }}</span>
        <div class="remind-actions">
          <button class="remind-btn">
            <img class="remind-btn-icon" src="/static/MyImage/edit.png" /> 生成日程提醒ICS
          </button>
          <button class="remind-btn delete" @click="deleteReminder(item.rid)">
            <img class="remind-btn-icon" src="/static/MyImage/logout.png" />
            删除
          </button>
        </div>
      </div>
      <div class="remind-info">
        <span class="remind-cycle"
          >每{{ item.intervalue }} {{ item.freq }} / {{ item.units }} {{ item.dose }}</span
        >
        <span class="remind-time">
          <img class="remind-time-icon" src="/static/MyImage/history.png" />
          {{ dayjs(item.dtstart).format('YYYY-MM-DD') }} -
          {{ dayjs(item.dtend).format('YYYY-MM-DD') }}
        </span>
      </div>
    </div>

    <!-- 添加提醒按钮 -->
    <div class="add-remind-btn" @click="goToTjyytx">
      <span class="add-icon">＋</span>添加用药提醒
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { getYytxListData, deleteYytxData } from '@/https/HomeHTTP'
import { onLoad } from '@dcloudio/uni-app'
import { useMemberStore } from '@/store'
import dayjs from 'dayjs'
const memberStore = useMemberStore()
const reminders = ref([])
const getYytxList = async () => {
  const res = await getYytxListData(memberStore.userUid)
  reminders.value = res.data
  console.log(reminders.value)
}
onLoad(async () => {
  await Promise.all([getYytxList()])
})
const goToYpxx = () => {
  uni.navigateTo({
    url: '/pages/medical/medical',
  })
}
const goToTjyytx = () => {
  uni.navigateTo({
    url: '/pages/component/tjyytx/tjyytx',
  })
}
const deleteReminder = async (rid: string) => {
  console.log(rid)
  uni.showModal({
    title: '提示',
    content: '确定删除该用药提醒吗？',
    success: async (res) => {
      if (res.confirm) {
        const res = await deleteYytxData(rid)
        console.log(res)
        if (res.code === 200) {
          uni.showToast({
            title: '删除成功',
            icon: 'success',
          })
          await getYytxList()
        }
      }
    },
  })
}
</script>

<style lang="scss" scoped>
.yytx-container {
  background: #fff;
  min-height: 100vh;
  font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
  padding: 0;
}
.search-bar {
  display: flex;
  align-items: center;
  border: 2px solid #3ec99e;
  border-radius: 12px;
  margin: 12px 12px 0 12px;
  padding: 0 12px;
  height: 48px;
  background: #fff;
}
.search-icon {
  width: 28px;
  height: 28px;
  margin-right: 8px;
}
.search-input {
  border: none;
  outline: none;
  font-size: 18px;
  flex: 1;
  background: transparent;
  color: #bbb;
}
.section-title {
  display: flex;
  align-items: center;
  margin: 24px 0 18px 18px;
}
.section-bar {
  width: 6px;
  height: 24px;
  background: #3ec99e;
  border-radius: 3px;
  margin-right: 10px;
  display: inline-block;
}
.section-title-text {
  font-size: 22px;
  font-weight: bold;
  color: #222;
}
.remind-card {
  background: linear-gradient(90deg, #1fe7a6 0%, #3ec99e 100%);
  border-radius: 16px;
  box-shadow: 0 2px 10px 0 #e6f7e6;
  margin: 18px 10px 0 10px;
  padding: 18px 22px 14px 22px;
  color: #fff;
  font-weight: bold;
  position: relative;
  display: flex;
  flex-direction: column;
}
.remind-row {
  display: flex;
  align-items: flex-start;
  margin-bottom: 6px;
}
.remind-bar {
  width: 6px;
  height: 28px;
  background: #fff;
  border-radius: 3px;
  margin-right: 14px;
  margin-top: 2px;
  display: inline-block;
}
.remind-title {
  font-size: 16px;
  font-weight: bold;
  margin-right: 18px;
  margin-top: 6px;
  color: rgb(0, 0, 0);
}
.remind-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: -6px;
}
.remind-btn {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  border: none;
  border-radius: 18px;
  font-size: 15px;
  font-weight: bold;
  padding: 4px 16px 4px 10px;
  display: flex;
  align-items: center;
  cursor: pointer;
  box-shadow: 0 2px 8px 0 #b6f5b6;
  transition: background 0.2s;
  margin-left: 0;
  margin-right: 0;
  &:hover {
    background: rgba(255, 255, 255, 0.32);
  }
}
.remind-btn.delete {
  background: rgba(79, 192, 141, 0.13);
  color: #fff;
}
.remind-btn-icon {
  width: 18px;
  height: 18px;
  margin-right: 5px;
}
.remind-info {
  display: flex;
  align-items: center;
  margin-top: 2px;
  font-size: 16px;
  font-weight: normal;
  gap: 18px;
}
.remind-cycle {
  margin-right: 0;
  font-weight: bold;
  color: #fff;
  font-size: 15px;
  color: rgb(0, 0, 0);
}
.remind-time {
  display: flex;
  align-items: center;
  font-weight: bold;
  color: #222;
  font-size: 16px;
  background: rgba(255, 255, 255, 0.18);
  border-radius: 12px;
  padding: 2px 12px 2px 8px;
  margin-left: 0;
}
.remind-time-icon {
  width: 16px;
  height: 16px;
  margin-right: 4px;
  vertical-align: middle;
}
.add-remind-btn {
  margin: 28px auto 0 auto;
  text-align: center;
  color: #3ec99e;
  font-size: 20px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 38px;
}
.add-icon {
  font-size: 22px;
  font-weight: bold;
  margin-right: 6px;
}
</style>
